<?php
/**
 * Clase Base FN para BaseImStockMovimFN
 *
 * @package    
 * @subpackage BaseImStockMovimFN
 * @author     hans
 * @version    SVN:: BaseImStockMovimFN.class.php,v 1.1 2012-03-18 20:29:05 Hans.parra Exp $
 */

require_once($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/im/mov/BN/ImStockMovimBN.class.php');

class BaseImStockMovimFN extends ImStockMovimBN
{
  private static $fieldNames = array (
    'MovimId'=>'stkm_id', 'DocId'=>'stkm_docId', 'DocNum'=>'stkm_docNum', 'AlmId'=>'stkm_almId', 'ArtId'=>'stkm_artId', 'Secuencia'=>'stkm_secuencia', 'MovFecha'=>'stkm_movFecha', 'IngSal'=>'stkm_ing_sal', 'MovTipo'=>'stkm_movTipo', 'Cant'=>'stkm_cant', 'CantAnt'=>'stkm_cantAnt', 'Costo'=>'stkm_costo', 'DocReferenTipo'=>'stkm_docReferenTipo', 'DocReferenNro'=>'stkm_docReferenNro', 'CreaUsr'=>'stkm_creaUsr', 'CreaFecha'=>'stkm_creaFecha', 'ModifUsr'=>'stkm_modifUsr', 'ModifFecha'=>'stkm_modifFecha'  
  );

  public function select()
  {
    try
    {
      /*$tamPag = !property_exists($this, 'tamPag')?9999:$this->tamPag;
      $pagina = !property_exists($this, 'page')||$this->page==""?1:$this->page;
      $numeroRegistros = $this->selectNumRows();
      $limitInf = ($pagina-1)*$tamPag;*/

      $where = $this->buildConsult();
      //$orden = $this->buildOrderByField();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      //$sql = "SELECT stkm_id, stkm_docId, stkm_docNum, stkm_almId, stkm_artId, stkm_secuencia, stkm_movFecha, stkm_ing_sal, stkm_movTipo, stkm_cant, stkm_cantAnt, stkm_costo, stkm_docReferenTipo, stkm_docReferenNro, stkm_creaUsr, stkm_creaFecha, stkm_modifUsr, stkm_modifFecha FROM im_stock_movim WHERE 1 ".$where." ".$orden."  LIMIT ".$limitInf.",".$tamPag;
      $sql = "SELECT stkm_id, stkm_docId, stkm_docNum, stkm_almId, stkm_artId, stkm_secuencia, stkm_movFecha, stkm_ing_sal, stkm_movTipo, stkm_cant, stkm_cantAnt, stkm_costo, stkm_docReferenTipo, stkm_docReferenNro, stkm_crea_usr, stkm_crea_fecha, stkm_modif_usr, stkm_modif_fecha FROM im_stock_movim WHERE 1 ".$where;
      $sql .= " Order By stkm_id DESC ";
      $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      $a_tmp = array();
      if($num_rows>0){
        while ($o_valor = mysql_fetch_object($result)) {
          $a_tmp[] = $this->insertObjectBN($o_valor);
        }
      }
      
      return count($a_tmp)>0?$a_tmp:null;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function selectNumRows()
  {
    try
    {
      $where = $this->buildConsult();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT stkm_id, stkm_docId, stkm_docNum, stkm_almId, stkm_artId, stkm_secuencia, stkm_movFecha, stkm_ing_sal, stkm_movTipo, stkm_cant, stkm_cantAnt, stkm_costo, stkm_docReferenTipo, stkm_docReferenNro, stkm_creaUsr, stkm_creaFecha, stkm_modifUsr, stkm_modifFecha FROM im_stock_movim WHERE 1 ".$where;
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      
      return $num_rows;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildConsult()
  {
    try
    {
      $where = "";

      if($this->getMovimId()){ $where .= " AND stkm_id = '".$this->getMovimId()."'"; }
      if($this->getDocId()){ $where .= " AND stkm_docid = '".$this->getDocId()."'"; }
      if($this->getDocNum()){ $where .= " AND stkm_docnum = '".$this->getDocNum()."'"; }
      if($this->getAlmId()){ $where .= " AND stkm_almid = '".$this->getAlmId()."'"; }
      if($this->getArtId()){ $where .= " AND stkm_artid = '".$this->getArtId()."'"; }
      if($this->getSecuencia()){ $where .= " AND stkm_secuencia = '".$this->getSecuencia()."'"; }
      if($this->getMovFecha()){ $where .= " AND stkm_movfecha = '".$this->getMovFecha()."'"; }
      if($this->getIngSal()){ $where .= " AND stkm_ing_sal = '".$this->getIngSal()."'"; }
      if($this->getMovTipo()){ $where .= " AND stkm_movtipo = '".$this->getMovTipo()."'"; }
      if($this->getCant()){ $where .= " AND stkm_cant = '".$this->getCant()."'"; }
      if($this->getCantAnt()){ $where .= " AND stkm_cantant = '".$this->getCantAnt()."'"; }
      if($this->getCosto()){ $where .= " AND stkm_costo = '".$this->getCosto()."'"; }
      if($this->getDocReferenTipo()){ $where .= " AND stkm_docreferentipo = '".$this->getDocReferenTipo()."'"; }
      if($this->getDocReferenNro()){ $where .= " AND stkm_docreferennro = '".$this->getDocReferenNro()."'"; }
      if($this->getCreaUsr()){ $where .= " AND stkm_creausr = '".$this->getCreaUsr()."'"; }
      if($this->getCreaFecha()){ $where .= " AND stkm_creafecha = '".$this->getCreaFecha()."'"; }
      if($this->getModifUsr()){ $where .= " AND stkm_modifusr = '".$this->getModifUsr()."'"; }
      if($this->getModifFecha()){ $where .= " AND stkm_modiffecha = '".$this->getModifFecha()."'"; }
      return $where;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildOrderByField()
  {
    try
    {
      $order_by = "";

      if(property_exists($this,'sortCampos')){
      if($this->sortCampos != ""){
        $a_valCampos = explode(";", $this->sortCampos);
        $a_valtype   = explode(";", $this->sortType);
        $order_by = " ORDER BY ";
        $a_order = array();
        foreach ($a_valCampos as $key => $val){
          $sTmpOrder = "";
          if(array_key_exists($val, self::$fieldNames)){
            $sTmpOrder .= self::$fieldNames[$val];
            switch ($a_valtype[$key]){
              case 1: $sTmpOrder .= " ASC"; break;
              case 2: $sTmpOrder .= " DESC"; break;
            }
            $a_order[] = $sTmpOrder;
          }
        }
        $order_by .= implode(", ", $a_order);
      }
      }
      return $order_by;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* B�squeda del registro por la llave primaria */
  public function selectByPK()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT stkm_id, stkm_docId, stkm_docNum, stkm_almId, stkm_artId, stkm_secuencia, stkm_movFecha, stkm_ing_sal, stkm_movTipo, stkm_cant, stkm_cantAnt, stkm_costo, stkm_docReferenTipo, stkm_docReferenNro, stkm_creaUsr, stkm_creaFecha, stkm_modifUsr, stkm_modifFecha FROM im_stock_movim WHERE 1 AND stkm_id='".$this->getMovimId()."'; ";
        $result = mysql_query($sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      

      return $o_Propel?$this->insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
    $o_BaseImStockMovimFN = new ImStockMovimBN();
    $o_BaseImStockMovimFN->setMovimId($o_propel->stkm_id);
    $o_BaseImStockMovimFN->setDocId($o_propel->stkm_docId);
    $o_BaseImStockMovimFN->setDocNum($o_propel->stkm_docNum);
    $o_BaseImStockMovimFN->setAlmId($o_propel->stkm_almId);
    $o_BaseImStockMovimFN->setArtId($o_propel->stkm_artId);
    $o_BaseImStockMovimFN->setSecuencia($o_propel->stkm_secuencia);
    $o_BaseImStockMovimFN->setMovFecha($o_propel->stkm_movFecha);
    $o_BaseImStockMovimFN->setIngSal($o_propel->stkm_ing_sal);
    $o_BaseImStockMovimFN->setMovTipo($o_propel->stkm_movTipo);
    $o_BaseImStockMovimFN->setCant($o_propel->stkm_cant);
    $o_BaseImStockMovimFN->setCantAnt($o_propel->stkm_cantAnt);
    $o_BaseImStockMovimFN->setCosto($o_propel->stkm_costo);
    $o_BaseImStockMovimFN->setDocReferenTipo($o_propel->stkm_docReferenTipo);
    $o_BaseImStockMovimFN->setDocReferenNro($o_propel->stkm_docReferenNro);
    $o_BaseImStockMovimFN->setCreaUsr($o_propel->stkm_crea_usr);
    $o_BaseImStockMovimFN->setCreaFecha($o_propel->stkm_crea_fecha);
    $o_BaseImStockMovimFN->setModifUsr($o_propel->stkm_modif_usr);
    $o_BaseImStockMovimFN->setModifFecha($o_propel->stkm_modif_fecha);
    return $o_BaseImStockMovimFN;
  }
}